import axios from './axios'
import { ElMessage } from 'element-plus'

const urlTit = 'http://192.168.1.116:9991/api/'

//get请求
function getaxiosdata(url, params) {
    return new Promise((resolve, reject) => {
        return axios.get(urlTit + url + '/', { 
            params  
        }).then((res) => {
            resolve(res)
        }).catch(() => {
            reject()
            ElMessage.error(`接口${url}错误, 请联系开发人员!`)
        })
    })
}

//post patch delete请求
function changeaxiosdata(url, method, params) {
    return new Promise((resolve, reject) => {
        let id = ['patch', 'put', 'delete'].includes(method) ? params.id  + '/' : ''
        return axios({
            url: urlTit + url + '/' + id,
            method,
            data: params
        }).then((res) => {
            resolve(res)
        }).catch(() => {
            reject()
            ElMessage.error(`接口${url}错误, 请联系开发人员!`)
        })
    })
}

//传流图片之类的
function streamaxiosdata(url, method, params) {
    return new Promise((resolve, reject) => {
        let id = method == 'patch' ? params.getAll('id')[0]  + '/' : ''
        return axios({
            url: urlTit + url + '/' + id,
            method,
            data: params
        }).then((res) => {
            resolve(res)
        }).catch(() => {
            reject()
            ElMessage.error(`接口${url}错误, 请联系开发人员!`)
        })
    })
}

//导出 导出
function exportaxiosdata(url, params){
    return new Promise((resolve, reject) => {
        return axios({
            url: urlTit + url + '/',
            method: 'post',
            data: params,
            responseType: "blob"
        }).then((res) => {
            resolve(res)
        }).catch(() => {
            reject()
            ElMessage.error(`接口${url}错误, 请联系开发人员!`)
        })
    })
}

export default {
    urlTit,
    getaxiosdata,
    changeaxiosdata,
    streamaxiosdata,
    exportaxiosdata
}